package com.hbsi.demo2;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * @author 程奕红
 * @version 1.0
 */
public class ReflectDemo4 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        MyString myString=new MyString();
        System.out.println(myString);
        Class c1=Class.forName("com.hbsi.demo2.MyString");
        Field field1=c1.getDeclaredField("str1");
        Field field2=c1.getDeclaredField("str2");
        Field field3=c1.getDeclaredField("str3");
        Field[] fields = c1.getDeclaredFields();
        Constructor cons= c1.getConstructor();
        Object object=cons.newInstance();

        // 遍历字段
        for (Field field : fields) {
            if (field.getType().equals(String.class)) {
                field.setAccessible(true);

                String oldValue = (String) field.get(object);

                String newValue = oldValue.replace('a', 'b');

                field.set(object, newValue);
            }

        }

        System.out.println(myString);

    }
}
